package com.util;

import com.bean.DocDossierFileCatalog;
import com.bean.DossierFileCatalog;
import com.bean.SQLiteArgument;
import com.consts.Const;
import org.apache.commons.codec.binary.Base64;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Smile
 */
public class FileDownLoad {

    public static List<SQLiteArgument> downLoadDossierFile(DossierFileCatalog dossier) throws Exception {
        //        String url = ConfigUtil.Service_Address + Const.DownLoad_Dossier;

        //        String url = CsbUtil.SendRequest(Const.download_dossier.toString(),null);
        //        String url = null;
        //        System.out.println("卷宗下载地址：" + url);
        if (dossier == null) {
            throw new Exception("下载电子卷宗参数缺失");
        }

        Map<String, String> parameter = new HashMap<>();
        parameter.put("dwbm", dossier.getDWBM());
        parameter.put("wjxh", dossier.getWJXH());

        //        Map<String, Object> response = HttpUtil.sendPost(url, parameter);
        Map<String, Object> response = CsbUtil.SendRequest(Const.download_dossier, parameter);
        String base64Str = response.get("data").toString();
        if (StringUtils.isEmpty(base64Str)) {
            throw new Exception(String.format("电子卷宗【%s】，未读取到内容", dossier.getWJXSMC()));
        }
        dossier.setPDF(Const.pdf_prefix + base64Str);
        byte[] pdfBytes = Base64.decodeBase64(base64Str);

        dossier.setIMG(PdfUtil.Pdf2Image(pdfBytes));
        dossier.setTEXT(PdfUtil.getTextFromPdf(pdfBytes, 0, 1));
        return EvidenceMaterialServer.addDossierFile(dossier);
    }

    public static List<SQLiteArgument> downLoadDocDossierFile(DocDossierFileCatalog docdossier) throws Exception {
        Map<String, String> parameter = new HashMap<>();
        parameter.put("wsslbh", docdossier.getWSBH());

        //        Map<String, Object> response = HttpUtil.sendPost(url, parameter);
        Map<String, Object> response = CsbUtil.SendRequest(Const.download_docDossier, parameter);
        String base64Str = response.get("data").toString();
        if (StringUtils.isEmpty(base64Str)) {
            throw new Exception(String.format("文书卷宗【%s】，未读取到内容", docdossier.getWJMC()));
        }
        byte[] fileBytes = Base64.decodeBase64(base64Str);

        try {
            String pdfStr = null;
            Map<Integer, String> imgs = null;
            switch (docdossier.getWJKZM().toUpperCase()) {
                case ".PDF":
                    pdfStr = Const.pdf_prefix + base64Str;
                    imgs = PdfUtil.Pdf2Image(fileBytes);
                    break;
                case ".DOC":
                case ".DOCX":
                case "OFD":
                    byte[] pdfBytes = WordUtil.word2Pdf(fileBytes);
                    imgs = PdfUtil.Pdf2Image(pdfBytes);
                    pdfStr = Const.pdf_prefix + Base64.encodeBase64String(pdfBytes);
                    break;
                case ".PNG":
                case ".JPG":
                case ".BMP":
                    pdfStr = fileBytes.length > 0 ? Const.img_prefix + Base64.encodeBase64String(fileBytes) : null;
                    imgs = new LinkedHashMap<>();
                    Map<String, String> img = new HashMap<>();
                    imgs.put(1, pdfStr);
                    break;
            }
            docdossier.setPDF(pdfStr);
            docdossier.setIMG(imgs);
            return EvidenceMaterialServer.addDocDossierFile(docdossier);
        } catch (Exception ex) {
            throw ex;
        }
    }
}
